﻿using System;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.OfficeArt
{
    internal class OfficeArtFOPT : OfficeArtRecord
    {
        private OfficeArtRGFOPTE _fopt = new OfficeArtRGFOPTE();

        public override RecType RecordType
        {
            get { return RecType.OfficeArtFOPT; }
        }

        protected override void ReadNoHeader(OfficeArtReadContext context)
        {
#if DEBUG
            var position = context.Reader.BaseStream.Position;
#endif
            Contract.Assert(rhrecVer == 0x03);
            Contract.Assert(rhrecType == RecType.OfficeArtFOPT);
            _fopt = new OfficeArtRGFOPTE();
            _fopt.Read(context.Reader, rhrecInstance, rhrecLen);
#if DEBUG
            Contract.Assert(context.BaseStream.Position - position == rhrecLen);
#endif
        }

        protected override byte Version
        {
            get { return 0x03; }
        }


        protected override int ByteLength
        {
            get { return _fopt.ByteLength; }
        }

        protected override void WriteNoHeader(OfficeArtWriteContext context)
        {
#if DEBUG
            var position = context.BaseStream.Position;
#endif
            Contract.Assert(rhrecVer == 0x03);
            Contract.Assert(rhrecType == RecType.OfficeArtFOPT);
            _fopt.Write(context.Writer);
#if DEBUG
            Contract.Assert(context.BaseStream.Position - position == rhrecLen);
#endif
        }
        protected override void PrepareHeader(ref OfficeArtRecordHeader rh)
        {
            rh.recInstance = this._fopt.Count;
        }

        public int? GetOptionalIntegerProperty(OpId id)
        {
            return _fopt.GetOptionalIntegerProperty(id);
        }

        public void AddProperty(OfficeArtFOPTE property)
        {
            _fopt.AddProperty(property);
        }
        public void AddProperty (OpId opid, object value)
        {
            this.AddProperty(new OfficeArtFOPTE(opid, value));
        }

        public string GetOptionalStringProperty(OpId pibName)
        {
            return _fopt.GetOptionalStringProperty(pibName);
        }
    }
}